package com.abner;

import org.openjdk.jol.info.ClassLayout;

/**
 * @author: guochao.bj@fang.com
 * @createDate: 2022/7/8 18:13
 */
public class MyObjectTest {


    public static void main(String[] args) throws InterruptedException {
        Person person = new Person();
        /**
         * 寻址`loss due to the next object alignment`
         */
        System.out.println("无状态001->"+(ClassLayout.parseInstance(person).toPrintable()));

        //JVM启动默认不启动偏向锁，有偏向延迟4s，休眠一会（也可以添加启动参数关闭偏向延迟），否则上来直接是轻量锁
        Thread.sleep(5000);
        final Person person2 = new Person();
        System.out.println("启用偏向锁101->"+(ClassLayout.parseInstance(person2).toPrintable()));

        for (int i = 0; i < 2; i++) {
            synchronized (person2){
                System.out.println("偏向锁101(线程id)->"+(ClassLayout.parseInstance(person2).toPrintable()));
            }
            System.out.println("偏向锁101(线程id)->"+(ClassLayout.parseInstance(person2).toPrintable()));
        }

        new Thread(() -> {
            synchronized (person2) {
                System.out.println("轻量级锁00->" + (ClassLayout.parseInstance(person2).toPrintable()));
                try {
                    System.out.println("sleep 3 秒");
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("轻量->重量升级锁10->" + (ClassLayout.parseInstance(person2).toPrintable()));
            }
        }).start();

        new Thread(() -> {
            synchronized (person2) {
                System.out.println("重量级锁10->" + (ClassLayout.parseInstance(person2).toPrintable()));
            }
        }).start();


    }




}


